﻿using DataBase;
using DataBase.Test;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApp1.Services
{
    public class UdpGetService
    {
        public UdpClient Client { get; set; }
        public UdpGetService()
        {
           
        }

        public async Task Start()
        {
            Client = new UdpClient(9210);
            Client.Client.ReceiveTimeout =  1000;
            Client.Client.SendTimeout = 1000;
            while (true)
            {
                try
                {
                    IPEndPoint point = new IPEndPoint(IPAddress.Any, 0);
                    //var socket = Client.Client.Accept();
                    byte[] data = Client.Receive(ref point);//通过point确定数据来自哪个ip的哪个端口号 返回值是一个字节数组，就是我们的数据
                    //byte[] result = Client.
                    string message = Encoding.UTF8.GetString(data);
                    Client.SendAsync(UTF8Encoding.UTF8.GetBytes("readok"), point);
                    //Client.Close();
                    //Client = new UdpClient(9210);
                    
                    Console.WriteLine("收到了消息：" + message);
                    
                    Analysis(message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                //Console.WriteLine("休眠10s");
                await Task.Delay(1 * 1000);
                //Console.WriteLine("休眠10s结束");

            }

        }


        public void Analysis(string data)
        {
            Regex regex = new Regex(@"\|");
            var datas = regex.Split(data);
            if (datas.Length == 17)
            {
                var insertModel = new TEarthNail()
                {
                    TerminalId = datas[1],
                    CreateTime = DateTime.Now,
                    Time = DateTime.Now,
                    IsVibrate = GetBool(datas[2]),
                    Longitude = (Convert.ToDecimal(datas[3]) / 100),
                    Latitude = (Convert.ToDecimal(datas[4]) / 100),
                    X = Convert.ToInt32(data[5]),
                    Y = Convert.ToInt32(datas[6]),
                    Z = Convert.ToInt32(datas[7]),
                    Sign = Convert.ToInt32(datas[8]),
                    Voltage = (Convert.ToDecimal(datas[9]) / 1000),
                    Power = Convert.ToInt32(datas[10]),
                    IsCharge = GetBool(datas[11]),
                    Temp = Math.Round(Convert.ToDecimal(datas[12]), 2),
                    WebStatus = GetBool(datas[13]),
                    NBNo = datas[14],
                    VersionNo = datas[15],
                };
                insertModel.DeviceId = Convert.ToInt64(insertModel.TerminalId, 16);
                FreeSqlService freeSqlService = new FreeSqlService();
                freeSqlService.Test.Insert(insertModel).ExecuteAffrows();
                Console.WriteLine("插入地钉数据");
                Console.WriteLine(JsonConvert.SerializeObject(insertModel));
            }
            else
            {
                Console.WriteLine("此为振动数据包，不存入数据库");
            }

            
        }

        public bool GetBool(string msg)
        {
            if (msg == "0")
            {
                return false;
            }
            else
            {
                return true;
            }
        }

    }
}
